package com.guojiang.guoaicodemother.core.saver;

import com.guojiang.guoaicodemother.ai.model.HtmlCodeResult;
import com.guojiang.guoaicodemother.ai.model.MultiFileCodeResult;
import com.guojiang.guoaicodemother.exception.BusinessException;
import com.guojiang.guoaicodemother.exception.ErrorCode;
import com.guojiang.guoaicodemother.model.enums.CodeGenTypeEnum;

import java.io.File;

/**
 * 代码文件保存执行器
 */
public class CodeFileSaverExecutor {

    private final static HtmlCodeFileSaverTemplate htmlCodeFileSaverTemplate = new HtmlCodeFileSaverTemplate();

    private final static MultiFileCodeFileSaverTemplate multiFileCodeFileSaverTemplate = new MultiFileCodeFileSaverTemplate();

    /**
     * 根据代码生成类型执行代码保存器
     *
     * @param codeResult
     * @param codeGenTypeEnum
     * @param appId
     * @return File
     */
    public static File executeCodeSaver(Object codeResult, CodeGenTypeEnum codeGenTypeEnum, Long appId) {
        return switch (codeGenTypeEnum) {
            case HTML -> htmlCodeFileSaverTemplate.saveCodeFile((HtmlCodeResult) codeResult, appId);
            case MULTI_FILE -> multiFileCodeFileSaverTemplate.saveCodeFile((MultiFileCodeResult) codeResult, appId);
            default ->
                    throw new BusinessException(ErrorCode.PARAMS_ERROR, "不支持代码文件保存类型：" + codeGenTypeEnum.getText());
        };
    }
}
